package com.example.administrator.taxicar.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.example.administrator.taxicar.R;
import com.example.administrator.taxicar.utils.Constants;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;

import java.io.File;

/**
 * Created by Administrator on 2016/4/29.
 */
public class TouxiangActivity extends Activity implements View.OnClickListener {
    private ImageView ivCropImage;
    ActionSheetDialog dialog;
    String path;

    public TouxiangActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //声明使用自定义标题
        setContentView(R.layout.activity_touxiang_layout);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.touxiang_title_layout);//自定义布局赋

        findViewById(R.id.title_left_img).setOnClickListener(this);
        findViewById(R.id.touxiang_xuanze).setOnClickListener(this);
        ivCropImage= (ImageView) findViewById(R.id.touxiang_img);

        SharedPreferences sharedPreferences= getSharedPreferences("feil_picture", Activity.MODE_PRIVATE);
        String path =sharedPreferences.getString("path", "");
        if(path!=null){
            Bitmap b = BitmapFactory.decodeFile(path);
            if (b != null) {
                ivCropImage.setImageBitmap(b);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_img:
                if(dialog!=null){
                    dialog.dismiss();
                }
                Intent intent =new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("path", path);//添加要返回给页面1的数据
                intent.putExtras(bundle);
                setResult(Constants.TOUXIANG_ACTIVITY,intent);
                finish();
                break;
            case R.id.touxiang_xuanze:
                ActionSheetDialogNoTitle();
                break;
        }
    }

    private void ActionSheetDialogNoTitle() {
        final String[] stringItems = {"拍照", "从手机相册选择"};
        dialog = new ActionSheetDialog(TouxiangActivity.this, stringItems, null);
        dialog.isTitleShow(false).show();

        dialog.setOnOperItemClickL(new OnOperItemClickL() {
            @Override
            public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position==0){
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    // 下面这句指定调用相机拍照后的照片存储的路径
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Constants.PHOTONAME)));
                    startActivityForResult(intent, Constants.TAKE_PHOTO);
                }else if(position==1){
                    // 相册中选择
                    Intent intent = new Intent(Intent.ACTION_PICK, null);
                    intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                    startActivityForResult(intent, Constants.PICK_PHOTO);
                }
                dialog.dismiss();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("tag","requestCode:"+requestCode+",resultCode:"+resultCode+",data="+data);
        if (requestCode == Constants.TAKE_PHOTO && resultCode == RESULT_OK) {
            Intent intent = new Intent(TouxiangActivity.this, ImageCropActivity.class);
            intent.putExtra("PHOTO_PATH",Constants.PHOTONAME);
            startActivityForResult(intent, Constants.CROP_BEAUTY);
        }
        if (requestCode == Constants.PICK_PHOTO && resultCode == RESULT_OK) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            Intent intent = new Intent(TouxiangActivity.this, ImageCropActivity.class);
            intent.putExtra("PHOTO_PATH",picturePath);
            startActivityForResult(intent, Constants.CROP_BEAUTY);
        }
        if (requestCode == Constants.CROP_BEAUTY && resultCode == RESULT_OK) {
            if (data != null) {
                 path = data.getStringExtra("path");
                Bitmap b = BitmapFactory.decodeFile(path);
                if (b != null) {
                    ivCropImage.setImageBitmap(b);
                }
            }
        }
    }

}